import 'package:flutter/material.dart';
import 'package:flutter_demo/config/colors.dart';
import 'package:flutter_demo/config/dimens.dart';
import 'package:flutter_demo/util/utils.dart';

class Toolbar extends StatelessWidget implements PreferredSizeWidget {
  final Widget leading;
  final bool automaticallyImplyLeading;
  final Widget title;
  final List<Widget> actions;
  final Widget flexibleSpace;
  final PreferredSizeWidget bottom;
  final double elevation;
  final Color backgroundColor;
  final Brightness brightness;
  final IconThemeData iconTheme;
  final TextTheme textTheme;
  final bool primary;
  final bool centerTitle;
  final double titleSpacing;
  final double toolbarOpacity;
  final double bottomOpacity;
  final bool hasLine;

  Toolbar({
    Key key,
    this.leading,
    this.automaticallyImplyLeading = true,
    this.title,
    this.actions,
    this.flexibleSpace,
    this.bottom,
    this.elevation = 0,
    this.backgroundColor,
    this.brightness,
    this.iconTheme,
    this.textTheme,
    this.primary = true,
    this.centerTitle = true,
    this.titleSpacing = NavigationToolbar.kMiddleSpacing,
    this.toolbarOpacity = 1.0,
    this.bottomOpacity = 1.0,
    this.hasLine = true,
    double height = 48,
  })  : assert(automaticallyImplyLeading != null),
        assert(elevation != null),
        assert(primary != null),
        assert(titleSpacing != null),
        assert(toolbarOpacity != null),
        assert(bottomOpacity != null),
        preferredSize = Size.fromHeight(height),
        super(key: key);

  @override
  // TODO: implement preferredSize
  final Size preferredSize;

  @override
  Widget build(BuildContext context) {
    var theme = Theme.of(context);
    var brightness = this.brightness ?? theme.primaryColorBrightness;
    final ModalRoute<dynamic> parentRoute = ModalRoute.of(context);
    final bool canPop = parentRoute?.canPop ?? false;

    var leading = this.leading;
    if (leading == null && canPop && automaticallyImplyLeading) {
      leading = _ReturnButton(
          brightness != Brightness.light ? Colors.white : Colors.black);
    }
    return Column(
      children: [
        Expanded(
          child: AppBar(
            leading: leading,
            automaticallyImplyLeading: automaticallyImplyLeading,
            title: title,
            actions: actions,
            flexibleSpace: flexibleSpace,
            bottom: bottom,
            elevation: elevation,
            backgroundColor: backgroundColor ??
                theme.primaryColor ??
                (brightness == Brightness.light ? Colors.white : Colors.black),
            brightness: brightness,
            iconTheme: iconTheme,
            textTheme: textTheme ??
                theme.textTheme.copyWith(
                  title: TextStyle(
                    color: brightness != Brightness.light
                        ? Colors.white
                        : Colors.black,
                    fontSize: Dimens.font_sp36,
                    fontWeight: FontWeight.w400,
                  ),
                ),
            primary: primary,
            centerTitle: centerTitle,
            titleSpacing: titleSpacing,
            toolbarOpacity: toolbarOpacity,
            bottomOpacity: bottomOpacity,
          ),
        ),
        Visibility(
            visible: hasLine,
            child: Container(
              height: Dimens.dHeight2,
              color: Colours.lineColor,
            )),
      ],
    );
  }
}

class _ReturnButton extends StatelessWidget {
  final Color color;

  _ReturnButton(this.color);

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon:  Image.asset(Utils.getImagePath('common/goback.png'),color: Colors.black,),

//      Icon(
//        Icons.arrow_back_ios,
//        size: Dimens.size_arrow_back,
//        color: color,
//      ),
      iconSize: Dimens.dWidth44,
      tooltip: MaterialLocalizations.of(context).backButtonTooltip,
      onPressed: () {
        Navigator.maybePop(context);
      },
    );
  }
}
